﻿using System;
using System.Data;
using System.Collections;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.BIZ;
using AnyOffice.Model;
using log4net;
using System.Reflection;

namespace AnyOffice.general.rms.roll_file
{
    public partial class _new : ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);


        protected override void OnInit(EventArgs e)
        {          
            this.btnRemoveFile.Click += new EventHandler(this.btnRemoveFile_Click);
            this.btnAddFile.Click += new EventHandler(this.btnAddFile_Click);
            this.btnSubmit.Click += new EventHandler(this.btnSubmit_Click);
            base.OnInit(e);
        }

 
        private void btnAddFile_Click(object sender, EventArgs e)
        {
            if (this.fileAttach.PostedFile.ContentLength > 0)
            {
                try
                {
                    string uniqueTempPath = this.ViewState["UniqueTempPath"].ToString();
                    if (uniqueTempPath == "")
                    {
                        uniqueTempPath = AttachmentBiz.GetUniqueTempPath();
                        this.ViewState["UniqueTempPath"] = uniqueTempPath;
                    }
                    Upload upload = new Upload();
                    upload.AutoCreateDirectory = true;
                    upload.OverwriteFile = true;
                    upload.DestinationPath = uniqueTempPath;
                    ConfigManager manager = new ConfigManager();
                    upload.MaxSize = manager.AttachMaxSize;
                    upload.GetFile(this.fileAttach.PostedFile);
                    upload.DoUpload();
                    this.ViewState["TotalSize"] = Convert.ToInt32(this.ViewState["TotalSize"]) + upload.FileSize;
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    list.Add(Path.GetFileName(this.fileAttach.PostedFile.FileName));
                    this.ViewState["FileList"] = list;
                    this.ViewState["FileNums"] = Convert.ToInt32(this.ViewState["FileNums"].ToString()) + 1;
                    this.ltbFileList.Items.Add(Path.GetFileName(this.fileAttach.PostedFile.FileName));
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) != 0)
                    {
                        this.ltbFileList.Rows = Convert.ToInt32(this.ViewState["FileNums"].ToString());
                    }
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) > 0)
                    {
                        this.pnlAttach.Visible = true;
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                }
            }
        }
        private void btnRemoveFile_Click(object sender, EventArgs e)
        {
            if (this.ltbFileList.Items.Count != 0)
            {
                try
                {
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    list.Remove(this.ltbFileList.SelectedItem.Text);
                    this.ViewState["FileList"] = list;
                    string fileName = this.ViewState["UniqueTempPath"].ToString() + this.ltbFileList.SelectedItem.Text;
                    FileInfo info = new FileInfo(fileName);
                    if (info.Exists)
                    {
                        this.ViewState["TotalSize"] = Convert.ToInt32(this.ViewState["TotalSize"]) - info.Length;
                        if (File.Exists(fileName))
                        {
                            File.Delete(fileName);
                        }
                    }
                    this.ViewState["FileNums"] = Convert.ToInt32(this.ViewState["FileNums"].ToString()) - 1;
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) >= 1)
                    {
                        this.ltbFileList.Rows = Convert.ToInt32(this.ViewState["FileNums"].ToString());
                    }
                    this.ltbFileList.Items.Remove(this.ltbFileList.SelectedItem.Text);
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) == 0)
                    {
                        if (Directory.Exists(this.ViewState["UniqueTempPath"].ToString()))
                        {
                            Directory.Delete(this.ViewState["UniqueTempPath"].ToString());
                        }
                        this.ViewState["UniqueTempPath"] = "";
                        this.pnlAttach.Visible = false;
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                }
            }
        }
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            string attachmentId = "";
            try
            {
                if (this.ViewState["UniqueTempPath"].ToString() != "")
                {
                    string uniquePath = AttachmentBiz.GetUniquePath(out attachmentId);
                    Directory.Move(this.ViewState["UniqueTempPath"].ToString(), uniquePath);
                    AttachmentData data = new AttachmentData();
                    AttachmentDao dao = new AttachmentDao();
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    for (int i = 0; i < list.Count; i++)
                    {
                        FileInfo info = new FileInfo(uniquePath + list[i].ToString());
                        if (info.Exists)
                        {
                            data.Type = 0;
                            data.AttachmentId = attachmentId;
                            data.Name = info.Name;
                            data.AddTime = DateTime.Now;
                            data.TotalSize = (int)info.Length;
                            data.UserId = base.User.Identity.Name;
                            dao.Insert(base.Conn, data);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("", exception);
                }
            }
            RmsFileData data2 = new RmsFileData();
            data2.FileCode = this.txtFileCode.Text;
            if (this.txtFileSubject.Text != string.Empty)
            {
                data2.FileSubject = this.txtFileSubject.Text;
            }
            data2.FileTitle = this.txtFileTitle.Text;
            if (this.txtFileTitle0.Text != string.Empty)
            {
                data2.FileTitle0 = this.txtFileTitle0.Text;
            }
            if (this.txtSendUnit.Text != string.Empty)
            {
                data2.SendUnit = this.txtSendUnit.Text;
            }
            if (this.txtSendDate.Value != string.Empty)
            {
                data2.SendDate = Convert.ToDateTime(this.txtSendDate.Value);
            }
            if (this.ddlSecret.SelectedValue != string.Empty)
            {
                data2.Secret = this.ddlSecret.SelectedValue;
            }
            if (this.ddlUrgency.SelectedValue != string.Empty)
            {
                data2.Urgency = this.ddlUrgency.SelectedValue;
            }
            if (this.ddlFileKind.SelectedValue != string.Empty)
            {
                data2.FileKind = this.ddlFileKind.SelectedValue;
            }
            if (this.ddlFileType.SelectedValue != string.Empty)
            {
                data2.FileType = this.ddlFileType.SelectedValue;
            }
            if (this.txtFilePage.Text != string.Empty)
            {
                data2.FilePage = this.txtFilePage.Text;
            }
            if (this.txtPrintPage.Text != string.Empty)
            {
                data2.PrintPage = this.txtPrintPage.Text;
            }
            if (this.txtRemark.Text != string.Empty)
            {
                data2.Remark = this.txtRemark.Text;
            }
            if (this.ddlRollId.SelectedValue != string.Empty)
            {
                data2.RollId = Convert.ToInt32(this.ddlRollId.SelectedValue);
            }
            if (attachmentId != string.Empty)
            {
                data2.AttachmentId = attachmentId;
            }
            new RmsFileDao().Insert(base.Conn, data2);
            this.pnlMain.Visible = false;
            this.litMessage.Text = base.ShowPrompt("文件新建成功！", "index1.aspx");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.ddlSecret.DataSource = new RmsCodeDao().SelectAll(base.Conn, RmsCodeDao.CodeType.Secret);
                this.ddlSecret.DataBind();
                this.ddlSecret.Items.Insert(0, new ListItem("", ""));
                this.ddlUrgency.DataSource = new RmsCodeDao().SelectAll(base.Conn, RmsCodeDao.CodeType.Urgency);
                this.ddlUrgency.DataBind();
                this.ddlUrgency.Items.Insert(0, new ListItem("", ""));
                this.ddlFileKind.DataSource = new RmsCodeDao().SelectAll(base.Conn, RmsCodeDao.CodeType.FileKind);
                this.ddlFileKind.DataBind();
                this.ddlFileKind.Items.Insert(0, new ListItem("", ""));
                this.ddlFileType.DataSource = new RmsCodeDao().SelectAll(base.Conn, RmsCodeDao.CodeType.FileType);
                this.ddlFileType.DataBind();
                this.ddlFileType.Items.Insert(0, new ListItem("", ""));
                this.ddlRollId.DataSource = new RmsRollDao().SelectOpen(base.Conn);
                this.ddlRollId.DataBind();
                this.ddlRollId.Items.Insert(0, new ListItem("", ""));
                this.ViewState["FileNums"] = 0;
                this.ViewState["FileList"] = new ArrayList();
                this.ViewState["UniqueTempPath"] = "";
                this.ViewState["TotalSize"] = 0;
                this.btnAddFile.Attributes.Add("onclick", "return CheckFile();");
                this.btnSubmit.Attributes.Add("onclick", "return CheckForm();");
            }


        }
    }
}
